<?php
session_start();
include_once "../apps/ManagerIUDS.php";
$managerIUDS = new ManagerIUDS();

$petName = $_POST["pet_name"];
$petAge = $_POST["pet_age"];
$petSex = $_POST["pet_sex"];
$petWeight= $_POST["pet_weight"];
$speciesId = $_POST["species_id"];
$ownerId= $_POST["owner_id"];
$breedId = $_POST["breed_id"];

$petId = $_GET["pet_id"];
if($petId == "" || $petId == null){
	$petId = $_POST["pet_id"];
}

$mode = $_GET["mode"];
if($mode == "" || $mode == null){
	$mode = $_POST["mode"];
}
$info = $_GET["info"];
if($info == "" || $info == null){
	$info = $_POST["info"];
}

$page = $_GET['page'];
if (!isset($page) && $page == "") {
	$page = 1;
}

if($mode == "insert"){
	$arrData["pet_name"] = "".$petName."";
	$arrData["pet_age"] ="".$petAge."";
	$arrData["pet_sex"] ="".$petSex."";
	$arrData["pet_weight"] ="".$petWeight."";
	$arrData["species_id"] = "".$speciesId."";
	$arrData["breed_id"] = "".$breedId."";
	$arrData["owner_id"] = "".$ownerId."";
	$arrData["create_date"] = "".date("Y-m-d")."";
	$arrData["create_by"] = "".$_SESSION["user_name"]."";
	$managerIUDS->inserData($arrData, "pet");
	header("Location:../contents/pet_main.php");
}else if($mode == "delete"){
	$arrPrimaryKey["pet_id"] = "".$petId."";
	$managerIUDS->deleteData("pet", $arrPrimaryKey, "");
	header("Location:../contents/pet_main.php");
}else if($mode == "update"){
	$arrData["pet_name"] = "".$petName."";
	$arrData["pet_age"] ="".$petAge."";
	$arrData["pet_sex"] ="".$petSex."";
	$arrData["pet_weight"] ="".$petWeight."";
	$arrData["species_id"] = "".$speciesId."";
	$arrData["breed_id"] = "".$breedId."";
	$arrData["owner_id"] = "".$ownerId."";
	$arrData["update_date"] = "".date("Y-m-d")."";
	$arrData["update_by"] = "".$_SESSION["user_name"]."";
	$arrPrimaryKey["pet_id"] = "".$petId."";
	//print_r($arrData);
	$managerIUDS->updateData($arrData, "pet", $arrPrimaryKey, "");
	header("Location:../contents/pet_main.php");
}else if($mode == "search"){
	$petName = $_GET["pet_name"];
	$ownerName = explode(" ", $_GET["owner_name"]);
	$ownerFname = $ownerName[0];
	$ownerLname = $ownerName[1];
	if($info == "for_predicate"){	
		header("Location:../contents/pet_list.php?mode=search&page=".$page."&pet_name=".urlencode($petName)."&owner_fname=".urlencode($ownerFname)."&owner_lname=".urlencode($ownerLname));
	}else{	
		header("Location:../contents/pet_main.php?mode=search&page=".$page."&pet_name=".urlencode($petName)."&owner_fname=".urlencode($ownerFname)."&owner_lname=".urlencode($ownerLname));	
	}
}else if($mode == "listPetData"){
	$petData = array();
	$petName = $_GET["pet_name"];
	if($petName == "" || $petName == null){
		$petName = $_POST["pet_name"];
	}
	$petData = $managerIUDS->queryData("pet_name", "pet", " WHERE pet_name LIKE '%".$petName."%'", "2", "");
	echo json_encode($petData);
}else if($mode == "listOwnerNameData"){
	$ownerData = array();
	$ownerName = $_GET["owner_name"];
	if($ownerName == "" || $ownerName == null){
		$ownerName = $_POST["owner_name"];
	}
	$ownerData = $managerIUDS->queryData("fname,lname,owner_id", "owner", " WHERE fname LIKE '%".$ownerName."%'", "2", "");
	echo json_encode($ownerData);
}else if($mode == "listSpiciesByPetId"){}
?>